package cc.bluemc.modules.weixin.action;

import cc.bluemc.platform.comm.ActionResultMap;
import cc.bluemc.platform.comm.BaseAction;
import cc.bluemc.platform.jdbctemplatetool.exception.NoColumnAnnotationFoundException;
import cc.bluemc.platform.jdbctemplatetool.exception.NoIdAnnotationFoundException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 微信菜单操作 action
 * 
 * 
 */
@Controller
@RequestMapping(value="/wx/")
public class WeixinIndexAction extends BaseAction{
	private static final long serialVersionUID = 5769445710768047749L;
	private static Logger logger = LoggerFactory.getLogger(WeixinIndexAction.class);
	@ResponseBody
	@RequestMapping("login/{openid}")
	public ActionResultMap login(@PathVariable String openid) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
		/*/
		WxuserEntity wxuser;
		try {
			wxuser =getWxuser();
		} catch (Exception e) {
			
			wxuser = wxuserService.getByOpenidAndAppId(openid);
			Subject subject = getSubject(); 
	    	subject.getSession().setAttribute(WeixinConstant.SESSION_WXUSER,wxuser);
		}
		/*/
		Subject subject = SecurityUtils.getSubject();
	    UsernamePasswordToken token = new UsernamePasswordToken(openid,"");
	        //4、登录，即身份验证  
	    try {
			
	    	subject.login(token); 
	    	resultMap.setSuccess(true);
	    	resultMap.setMsg("微信用户登录成功");
		} catch (Exception e) {
			resultMap.setSuccess(false);
	    	resultMap.setMsg("微信用户登录失败");
		}
		return resultMap;
	    //*/
	}
	@ResponseBody
	@RequestMapping("invalid")
	public Map<String, String> invalid(HttpServletRequest request,String url){
//		String jsapiTicket= coreService.getJsTicket();
//		List<String> list = StringUtil.regexFindCharterItem(url, "openId=([^&]*)");
//		if(list.size()>0){
//			logger.info(list.get(0));
//			try {
//				UserInfo userInfo = WeChat.user.getUserInfo(coreService.getAccessToken(), list.get(0));
//				logger.info(JSON.toJSONString(userInfo));
//			} catch (Exception e) {
//			}
//		}
//		Map<String, String> map = WeChat.jsApiSign(jsapiTicket, url);
//		map.put("appId", ConfKit.get("AppId"));
//		return map;
		return null;
	}
}
